package cn.com.dashihui.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;

import cn.com.dashihui.kit.DirKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.ContentsCategory;
import cn.com.dashihui.web.service.ContentsCategoryService;
@RequiresAuthentication
public class ContentsCategoryController extends BaseController {
	private ContentsCategoryService ccs = new ContentsCategoryService();
	public void index() {
		render("index.jsp");
	}

	public void page() {
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, ccs.findByPage(pageNum, pageSize));
	}

	public void doSort(){
		Map<String, String[]> params = getParaMap();
		if (params != null && params.size() != 0 && params.containsKey("sortKey")) {
			String sortKey = params.get("sortKey")[0];
			Map<String, String> sortMap = new HashMap<String, String>();
			for (String key : params.keySet()) {
				if (!key.equals("sortKey")) {
					String id = key.replace(sortKey, "");
					String no = params.get(key)[0];
					if (StrKit.isBlank(no) || no.length() > 3) {
						no = "0";
					}
					sortMap.put(id, no);
				}
			}
			ccs.sortCategory(sortMap);
			renderSuccess();
			return;
		}
		renderFailed();
	}

	public void toAdd() {
		render("add.jsp");
	}

	public void doAdd(){
		UploadFile thumb=getFile("thumb");
		String name=getPara("name");
		Integer state=getParaToInt("state");
		if(StrKit.isBlank(name)){
			renderResult(1);
			return;
		}else if(state==null||state>1||state<0){
			renderResult(2);
			return;
			
		}else if(thumb==null){
			renderResult(3);
			return;
		}else{
			String thumbFileName;
			String dir=DirKit.getDir(DirKit.CONTENTS_CATEGORY);
			try{
				thumbFileName=uploadToFtp(dir,thumb);
			}catch(IOException e){
				e.printStackTrace();
				renderResult(4);
				return;
			}
			ContentsCategory cc=new ContentsCategory()
					.set("name",name)
					.set("state", state)
					.set("thumb",dir.concat(thumbFileName));
			if(ccs.AddCategory(cc)){
				renderSuccess();
				return;
			}
		}
		renderFailed();
	}
	public void toEdit(){
		int id=getParaToInt(0,0);
		if(id!=0){
			setAttr("object",ccs.findById(id));
		}
		render("edit.jsp");
		
	}
	public void doEdit(){
		UploadFile thumb=getFile("thumb");
		String id=getPara("id");
		String name=getPara("name");
		Integer state=getParaToInt("state");
		String thumbOld=getPara("thumbOld");
		if(StrKit.isBlank(name)){
			renderFailed();
			return;
		}else if(StrKit.isBlank(name)){
			renderResult(1);
			return;
		}else if(state==null||state>1||state<0){
			renderResult(2);
			return;
		}else{
			ContentsCategory cc=new ContentsCategory()
					.set("id",id)
					.set("name",name)
					.set("state", state);
			if(thumb!=null){
				String thumbFileName;
				String dir=DirKit.getDir(DirKit.CONTENTS_CATEGORY);
				try{
					thumbFileName=uploadToFtp(dir,thumb);
				}catch(IOException e){
					e.printStackTrace();
					renderResult(3);
					return;
				}
				
				cc.set("thumb",dir.concat(thumbFileName));
				if(ccs.editCategory(cc)){
					try{
						deleteFromFtp(thumbOld);
					}catch(IOException e){
					logger.error("更新时,旧图片"+thumbOld+"删除失败!");
						e.printStackTrace();
					}
					renderSuccess();
					return;
				}
				
				
			}else if(ccs.editCategory(cc)){
				renderSuccess();
				return;
			}

		}
		renderFailed();	
		
	}
	
public void doDelete(){
	int id =getParaToInt(0,0);
	if(ccs.findExistSubject(id)){
		renderResult(1);
		return;
		
	}
	
	if(id!=0){
		ContentsCategory cc=ccs.findById(id);
		if(StrKit.notBlank(cc.getStr("thumb"))){
			try{
				deleteFromFtp(cc.getStr("thumb"));
			}catch(IOException e){
				//
				logger.error("删除时,相关图片"+cc.getStr("thumb")+"删除失败");
				e.printStackTrace();
			}
		}
		if(ccs.deleteCategory(id)){
			renderSuccess();
			return;
		}
	}
	renderFailed();
}
	
	
	
	
	
	
	
	
	
	
	

}
